/**
 * 
 *
 * 
 *
 * 版权所有，侵权必究！
 */

package io.renren.common.utils;

import io.renren.common.constant.Dict;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;

/**
 * excel工具类
 *
 * @author Mark 
 */
public class ExcelUtils {

    /**
     * Excel导出
     *
     * @param response      response
     * @param fileName      文件名
     * @param list          数据List
     * @param pojoClass     对象Class
     */
    public static void exportExcel(HttpServletResponse response, String fileName, Collection<?> list,
                                     Class<?> pojoClass) throws IOException {
        if(StringUtils.isBlank(fileName)){
            //当前日期
            fileName = DateUtils.format(new Date());
        }

        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), pojoClass, list);
        response.setCharacterEncoding("UTF-8");
        response.setHeader("content-Type", "application/vnd.ms-excel");
        response.setHeader("Content-Disposition",
                "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xls");
        ServletOutputStream out = response.getOutputStream();
        workbook.write(out);
        out.flush();
    }

    /**
     * Excel导出，先sourceList转换成List<targetClass>，再导出
     *
     * @param response      response
     * @param fileName      文件名
     * @param sourceList    原数据List
     * @param targetClass   目标对象Class
     */
    public static void exportExcelToTarget(HttpServletResponse response, String fileName, Collection<?> sourceList,
                                     Class<?> targetClass) throws Exception {
        List targetList = new ArrayList<>(sourceList.size());
        for(Object source : sourceList){
            Object target = targetClass.newInstance();
            BeanUtils.copyProperties(source, target);
            targetList.add(target);
        }

        exportExcel(response, fileName, targetList, targetClass);
    }
    
    
    /**
     * excel导出转换通道类型与二维码类型
     * @param list
     * @return
     */
    public static List<Map<String, Object>> changeExcelTrscode( List<Map<String, Object>> list) {
    	for (Map<String, Object> map : list) {
			if(map.get(Dict.ORDERCHANNEL)!=null) {
				String orderchannel=map.get(Dict.ORDERCHANNEL)+"";
				switch (orderchannel){
					case "0":
						map.put(Dict.ORDERCHANNEL, "微信");
						break;
					case "1":
						map.put(Dict.ORDERCHANNEL, "支付宝");
						break;
					case "2":
						map.put(Dict.ORDERCHANNEL, "QQ支付 ");
						break;
					case "3":
						map.put(Dict.ORDERCHANNEL, "云闪付");
						break;
					case "4":
						map.put(Dict.ORDERCHANNEL, "网银转账");
						break;
				}
			}
			
			if(map.get(Dict.TRSCODE)!=null) {
				String trscode=map.get(Dict.TRSCODE)+"";
				switch (trscode){
					case Dict.WECHAT_DYNAMIC:
						map.put(Dict.TRSCODE, "动态收款码");
						break;
					case Dict.WECHAT_DYNAMICCODE_DYT:
						map.put(Dict.TRSCODE, "微信店员通");
						break;
					case Dict.WECHAT_FKPAY:
						map.put(Dict.TRSCODE, "微信福卡e支付");
						break;
					case Dict.WECHAT_XPOS:
						map.put(Dict.TRSCODE, "微信星pos");
						break;
					case Dict.ZFB_FIXED:
						map.put(Dict.TRSCODE, "支付宝固码");
						break;
					
					case Dict.ZFB_DYNAMIC:
						map.put(Dict.TRSCODE, "动态收款码");
						break;
						
					case Dict.ZFB_REDBAG:
						map.put(Dict.TRSCODE, "支付宝红包");
						break;
					case Dict.ZFB_INITIATIVE:
						map.put(Dict.TRSCODE, "加好友收款");
						break;
					case Dict.ZFB_DDBREDBAG:
						map.put(Dict.TRSCODE, "钉钉群红包");
						break;
					case Dict.ZFB_TRANSFER:
						map.put(Dict.TRSCODE, "支付宝转账");
						break;
					case Dict.ZFB_NEW_TRANSFER:
						map.put(Dict.TRSCODE, "支付宝新转账");
						break;
					case Dict.ZFB_QREDBAG:
						map.put(Dict.TRSCODE, "支付宝群红包");
						break;
					case Dict.ZFB_TRANSFER_DYNAMIC:
						map.put(Dict.TRSCODE, "支付宝转账2");
						break;
						
					case Dict.ZFB_TOBANK:
						map.put(Dict.TRSCODE, "支付宝转银行");
						break;
					case Dict.ZFB_FKPAY:
						map.put(Dict.TRSCODE, "支付宝福卡e支付");
						break;
					case Dict.ZFB_XPOS:
						map.put(Dict.TRSCODE, "支付宝星pos");
						break;
					case Dict.UNIONPAY_DYNAMIC:
						map.put(Dict.TRSCODE, "动态收款码");
						break;
				}
				 
			}
		}
		return list;
    }
}
